package example.service.impl;

import example.common.Result;
import example.dto.SelectionAddDTO;
import example.dto.SelectionListDTO;
import example.entity.Selection;
import example.entity.SelectionDistributeInfo;
import example.mapper.DepartmentMapper;
import example.mapper.SelectionDistributeInfoMapper;
import example.mapper.SelectionMapper;
import example.service.ISelectionService;
import example.vo.SelectionListVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SelectionServiceImpl implements ISelectionService {

    @Value("${file.selection_dir}")
    private String selectionDir;

    @Resource
    private SelectionMapper selectionMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private SelectionDistributeInfoMapper selectionDistributeInfoMapper;

    @Override
    public Result getSelection(SelectionListDTO selectionListDTO) {
        Long levelId = selectionListDTO.getLevelId();
        Long departmentId = selectionListDTO.getDepartmentId();
        if (levelId == 1L) {
            List<SelectionListVO> selectionList =  selectionMapper.getSelection();
            return Result.ok(selectionList);
        }
        List<SelectionListVO> selectionList = new ArrayList<>();
        List<Long> selectionIdList =  selectionDistributeInfoMapper.getSIdByDid(departmentId);
        for (Long selectionId : selectionIdList) {
            SelectionListVO selectionListVO = selectionMapper.getSelectionById(selectionId);
            selectionList.add(selectionListVO);
        }
        return Result.ok(selectionList);
    }

    @Override
    public Result addSelection(SelectionAddDTO selectionAddDTO) {
        selectionMapper.addSelection(selectionAddDTO);
        return Result.ok("添加选调事项成功！");
    }

    @Override
    public Result uploadSelectionPdf(MultipartFile file, Long selectionId) {
        String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
        String filePath = selectionDir + "\\" + fileName;
        File newFile = new File(filePath);
        newFile.getParentFile().mkdirs();
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            return Result.fail("上传失败！");
        }
        Selection selection = new Selection();
        selection.setId(selectionId);
        selection.setFileName(fileName);
        selection.setFilePath(filePath);
        selectionMapper.saveFile(selection);
        return Result.ok("文件上传成功！");
    }

    @Override
    public Result disSelection(Long selectionId, List<String> departmentNames) {
        try {
            for (String name :departmentNames) {
                Long departmentId = departmentMapper.getDepartmentIdByName(name);
                SelectionDistributeInfo selectionDistributeInfo = new SelectionDistributeInfo();
                selectionDistributeInfo.setSelectionId(selectionId);
                selectionDistributeInfo.setDepartmentId(departmentId);
                selectionDistributeInfoMapper.save(selectionDistributeInfo);
            }
            return Result.ok("分发成功！");
        } catch (Exception e) {
            return Result.fail("请不要重复分发！");
        }
    }
}
